import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QLineEdit
from PyQt5.QtGui import QPixmap, QPainter, QPen, QBrush, QFont
from PyQt5.QtCore import Qt, QRect, QTimer
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QScreen

class ScreenshotWidget(QWidget):
    def __init__(self, main_window):
        super().__init__()
        self.main_window = main_window
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setGeometry(0, 0, QApplication.desktop().width(), QApplication.desktop().height())
        self.start_pos = None
        self.end_pos = None
        self.is_drawing = False

    def paintEvent(self, event):
        if self.start_pos and self.end_pos:
            painter = QPainter(self)
            painter.setPen(QPen(Qt.red, 2, Qt.SolidLine))
            painter.setBrush(QBrush(Qt.NoBrush))
            rect = QRect(self.start_pos, self.end_pos)
            painter.drawRect(rect)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.start_pos = event.globalPos()
            self.is_drawing = True

    def mouseMoveEvent(self, event):
        if self.is_drawing:
            self.end_pos = event.globalPos()
            self.update()

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.end_pos = event.globalPos()
            self.is_drawing = False
            self.close()
            self.main_window.update_screenshot_area(self.start_pos, self.end_pos)

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Escape:
            self.close()

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_screenshot)
        self.timer.start(250)  # 更新频率为 250ms
        self.screenshot_area = (0, 0, 400, 1580)  # 默认截图区域为屏幕左下角 400x1580

    def initUI(self):
        self.setWindowTitle("EVE Online Warning Assistant")
        self.resize(400, 500)
        self.setStyleSheet("""
            background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1,
                                              stop: 0 #121212, stop: 1 #343434);
            color: white;
            font-family: 'Consolas';
        """)

        main_layout = QHBoxLayout(self)

        self.observation_box = QLabel(self)
        self.observation_box.setStyleSheet("""
            background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1,
                                              stop: 0 #222222, stop: 1 #444444);
            border: 1px solid #FF00FF;  # 粉色边框
            padding: 5px;
        """)
        self.observation_box.setFixedSize(200, 500)
        main_layout.addWidget(self.observation_box)

        right_layout = QVBoxLayout()

        self.start_button = QPushButton("开始", self)
        self.start_button.setStyleSheet("""
            background-color: white;  # 白色背景
            color: black;  # 黑色文字
            border: 2px solid #FF00FF;  # 粉色边框
            border-radius: 10px;
            font-size: 14px;
            padding: 5px;
        """)
        self.start_button.setFont(QFont("Consolas", 12))
        self.start_button.setFixedSize(200, 50)
        self.start_button.clicked.connect(self.on_start_clicked)
        right_layout.addWidget(self.start_button)

        self.link_wechat_button = QPushButton("链接微信", self)
        self.link_wechat_button.setStyleSheet("""
            background-color: white;  # 白色背景
            color: black;  # 黑色文字
            border: 2px solid #FF00FF;  # 粉色边框
            border-radius: 10px;
            font-size: 14px;
            padding: 5px;
        """)
        self.link_wechat_button.setFont(QFont("Consolas", 12))
        self.link_wechat_button.setFixedSize(200, 50)
        self.link_wechat_button.clicked.connect(self.on_link_wechat_clicked)
        right_layout.addWidget(self.link_wechat_button)

        self.end_button = QPushButton("结束", self)
        self.end_button.setStyleSheet("""
            background-color: white;  # 白色背景
            color: black;  # 黑色文字
            border: 2px solid #FF00FF;  # 粉色边框
            border-radius: 10px;
            font-size: 14px;
            padding: 5px;
        """)
        self.end_button.setFont(QFont("Consolas", 12))
        self.end_button.setFixedSize(200, 50)
        self.end_button.clicked.connect(self.on_end_clicked)
        right_layout.addWidget(self.end_button)

        self.adjust_screenshot_button = QPushButton("调整截图区域", self)
        self.adjust_screenshot_button.setStyleSheet("""
            background-color: white;  # 白色背景
            color: black;  # 黑色文字
            border: 2px solid #FF00FF;  # 粉色边框
            border-radius: 10px;
            font-size: 14px;
            padding: 5px;
        """)
        self.adjust_screenshot_button.setFont(QFont("Consolas", 12))
        self.adjust_screenshot_button.setFixedSize(200, 50)
        self.adjust_screenshot_button.clicked.connect(self.on_adjust_screenshot_clicked)
        right_layout.addWidget(self.adjust_screenshot_button)

        self.screenshot_area_input = QLineEdit(self)
        self.screenshot_area_input.setPlaceholderText("输入截图区域 (x,y,width,height)")
        self.screenshot_area_input.setStyleSheet("""
            background-color: white;  # 白色背景
            color: black;  # 黑色文字
            border: 2px solid #FF00FF;  # 粉色边框
            border-radius: 10px;
            font-size: 14px;
            padding: 5px;
        """)
        self.screenshot_area_input.setFont(QFont("Consolas", 12))
        self.screenshot_area_input.setFixedSize(200, 50)
        self.screenshot_area_input.returnPressed.connect(self.on_screenshot_area_input)
        right_layout.addWidget(self.screenshot_area_input)

        main_layout.addLayout(right_layout)

    def update_screenshot(self):
        if self.screenshot_area:
            screenshot = QScreen.grabWindow(QApplication.primaryScreen(), QApplication.desktop().winId(), self.screenshot_area[0], self.screenshot_area[1], self.screenshot_area[2], self.screenshot_area[3])
            screenshot.save("screenshot.png", "png")
            pixmap = QPixmap("screenshot.png")
            self.observation_box.setPixmap(pixmap)

    def on_start_clicked(self):
        print("开始按钮被点击")

    def on_link_wechat_clicked(self):
        print("链接微信按钮被点击")

    def on_end_clicked(self):
        print("结束按钮被点击")

    def on_adjust_screenshot_clicked(self):
        self.screenshot_widget = ScreenshotWidget(self)
        self.screenshot_widget.show()

    def on_screenshot_area_input(self):
        input_text = self.screenshot_area_input.text()
        try:
            x, y, width, height = map(int, input_text.split(','))
            self.screenshot_area = (x, y, width, height)
            self.update_screenshot()
            print(f"截图区域设置为: {self.screenshot_area}")
        except ValueError:
            print("输入格式错误，请输入四个整数，用逗号分隔 (x,y,width,height)")

    def update_screenshot_area(self, start_pos, end_pos):
        x1, y1 = start_pos.x(), start_pos.y()
        x2, y2 = end_pos.x(), end_pos.y()
        self.screenshot_area = (min(x1, x2), min(y1, y2), abs(x2 - x1), abs(y2 - y1))
        self.update_screenshot()
        print(f"截图区域设置为: {self.screenshot_area}")

def main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
